package com.youku.adSDK.view
{
   import flash.display.MovieClip;
   import com.youku.adSDK.ADEventDispather;
   import com.youku.adSDK.ADContext;
   import com.youku.adSDK.view.events.ADEvent;
   import com.youku.adSDK.YoukuAdEvent;
   import com.youku.adSDK.model.vo.GroupData;
   import com.youku.adSDK.YoukuAdManager;
   import flash.utils.getTimer;
   import flash.utils.setTimeout;
   import com.youku.adSDK.view.components.VideoAD;
   import com.youku.adSDK.model.vo.ADData;
   import com.youku.adSDK.view.events.VideoADEvent;
   import com.youku.adSDK.ADConstant;
   
   public class InsertMediator extends Object
   {
      
      public function InsertMediator(param1:ADEventDispather, param2:ADContext) {
         super();
         this._mainMc = param2.mainMc;
         this._context = param2;
         this._eventDispather = param1;
         this._dataArr = [];
         this._index = -1;
         this._lastIndex = -2;
      }
      
      public static const NAME:String = "InsertMediator";
      
      private var _mainMc:MovieClip;
      
      private var _eventDispather:ADEventDispather;
      
      private var _dataArr:Array;
      
      private var _index:int;
      
      private var _lastIndex:int;
      
      private var _width:Number = 0;
      
      private var _height:Number = 0;
      
      private var _barH:Number;
      
      private var _shutData:Boolean = false;
      
      private var _tipShowed:Boolean = false;
      
      private var _volume:Number = -1;
      
      private var _dataError:Boolean = false;
      
      private var _isPause:Boolean = true;
      
      private var _playedTime:Number = 0;
      
      private var _lastCountedTime:Number = 0;
      
      private const SHOW_TIPS_TIME_BEFORE:int = 10;
      
      private const AD_INTERVAL_TIME:int = 180000.0;
      
      private var _context:ADContext;
      
      public function init() : void {
         this._eventDispather.addEventListener(ADEvent.CHANGE_SIZE,this.onPlayerResize);
         this._eventDispather.addEventListener(ADEvent.COMMON_ERROR,this.onPlayerError);
         this._eventDispather.addEventListener(ADEvent.COMMON_RESET,this.onPlayerReset);
         this._eventDispather.addEventListener(ADEvent.CHANGE_VOLUME,this.onPlayerChangeVolume);
         this._eventDispather.addEventListener(ADEvent.PLAYER_PAUSE,this.onPause);
         this._eventDispather.addEventListener(ADEvent.PLAYER_RESUME,this.onResume);
         this._eventDispather.addEventListener(ADEvent.VIDEO_END,this.onPlayOver);
         this._eventDispather.addEventListener(YoukuAdEvent.AD_VIDEO_PLAY_COMPLETE,this.onAdPlayComplete);
         this._eventDispather.addEventListener(ADEvent.INSERT_DATA_LOADED,this.onADEvent);
         this._eventDispather.addEventListener(ADEvent.INSERT_DATA_LOAD_ERROR,this.onADEvent);
      }
      
      private function onADEvent(param1:ADEvent) : void {
         if(this._shutData)
         {
            return;
         }
         switch(param1.type)
         {
            case ADEvent.INSERT_DATA_LOADED:
               this.createAD(param1.data as GroupData);
               break;
            case ADEvent.INSERT_DATA_LOAD_ERROR:
               this.onDataError();
               break;
         }
      }
      
      private function onAdPlayComplete(param1:YoukuAdEvent) : void {
         this._playedTime = 0;
      }
      
      private function onPlayOver(param1:ADEvent) : void {
         this._isPause = true;
      }
      
      private function onResume(param1:ADEvent) : void {
         this._isPause = false;
      }
      
      private function onPause(param1:ADEvent) : void {
         this._isPause = true;
      }
      
      public function setPoints(param1:Array) : void {
         this.initData(param1);
      }
      
      private function onPlayerResize(param1:ADEvent) : void {
         this._width = param1.data.width;
         this._height = param1.data.height;
         this._barH = param1.data.cheight;
      }
      
      private function onPlayerReset(param1:ADEvent) : void {
         this.close();
      }
      
      private function onPlayerError(param1:ADEvent) : void {
         this.close();
      }
      
      private function close() : void {
         var _loc1_:* = 0;
         this._shutData = true;
         if(this._dataArr)
         {
            _loc1_ = 0;
            while(_loc1_ < this._dataArr.length)
            {
               if(this._dataArr[_loc1_].ad)
               {
                  this._dataArr[_loc1_].ad.dispose();
               }
               _loc1_++;
            }
         }
         this._dataArr = [];
         this._index = -1;
         this._lastIndex = -2;
      }
      
      private function onPlayerChangeVolume(param1:ADEvent) : void {
         this._volume = param1.data.volume;
      }
      
      private function initData(param1:Array) : void {
         var _loc2_:Array = [];
         var _loc3_:* = 0;
         while(_loc3_ < param1.length)
         {
            if(param1[_loc3_].ctype == "standard")
            {
               this._dataArr.push(
                  {
                     "time":Number(param1[_loc3_].startTime) / 1000,
                     "ad":null,
                     "loaded":false,
                     "played":false,
                     "playOverTime":null
                  });
               _loc2_.push(Number(param1[_loc3_].startTime) / 1000);
            }
            _loc3_++;
         }
         if(_loc2_.length)
         {
            this._eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_INSERT_POINT_LOADED,{"points":_loc2_}));
            this._eventDispather.addEventListener(ADEvent.VIDEO_PLAYING,this.onPlayerPlaying);
         }
      }
      
      private function onPlayerPlaying(param1:ADEvent) : void {
         var _loc12_:* = NaN;
         var _loc13_:* = false;
         var _loc14_:* = false;
         var _loc15_:* = false;
         if(!param1.data.nsTime || (YoukuAdManager.frontPlaying))
         {
            return;
         }
         var _loc2_:Number = getTimer();
         if(this._isPause)
         {
            this._lastCountedTime = _loc2_;
         }
         else
         {
            this._playedTime = this._playedTime + (_loc2_ - this._lastCountedTime);
            this._lastCountedTime = _loc2_;
         }
         if(this._isPause)
         {
            return;
         }
         var _loc3_:Number = param1.data.nsTime;
         var _loc4_:Array = [];
         var _loc5_:int = this._dataArr.length;
         var _loc6_:* = 0;
         while(_loc6_ < this._dataArr.length)
         {
            _loc12_ = Math.abs(this._dataArr[_loc6_].time - _loc3_);
            _loc4_.push(
               {
                  "n":_loc6_,
                  "span":_loc12_
               });
            _loc6_++;
         }
         if(!_loc4_.length)
         {
            return;
         }
         var _loc7_:int = _loc4_[0].n;
         var _loc8_:Number = _loc4_[0].span;
         _loc6_ = 0;
         while(_loc6_ < _loc4_.length)
         {
            if(_loc4_[_loc6_].span < _loc8_)
            {
               _loc8_ = _loc4_[_loc6_].span;
               _loc7_ = _loc4_[_loc6_].n;
            }
            _loc6_++;
         }
         if(_loc7_ > 0)
         {
            _loc13_ = _loc3_ < this._dataArr[_loc7_].time;
            _loc14_ = _loc3_ <= this._dataArr[_loc7_ - 1].time + 30;
            _loc15_ = this._dataArr[_loc7_ - 1].time + 30 < this._dataArr[_loc7_];
            if((_loc13_) && (_loc14_) && (_loc15_))
            {
               _loc7_--;
            }
         }
         if(this._playedTime < this.AD_INTERVAL_TIME)
         {
            return;
         }
         var _loc9_:Number = this._dataArr[_loc7_].time - _loc3_;
         var _loc10_:Boolean = _loc9_ >= 0 && _loc9_ <= 3 * 60 || _loc9_ <= 0 && _loc9_ >= -30;
         if(!this._dataArr[_loc7_].loaded && (_loc10_))
         {
            _loc6_ = 0;
            while(_loc6_ < _loc5_)
            {
               if(_loc6_ == _loc7_)
               {
                  this._dataArr[_loc6_].loaded = true;
               }
               else
               {
                  this._dataArr[_loc6_].loaded = false;
               }
               _loc6_++;
            }
            this._index = _loc7_;
            this._lastIndex = _loc7_;
            this._shutData = false;
            this._eventDispather.dispatchEvent(new ADEvent(ADEvent.DO_INSERT,
               {
                  "time":this._dataArr[_loc7_].time,
                  "no":_loc7_
               }));
         }
         var _loc11_:Boolean = Math.abs(_loc9_) <= 0.2 || _loc9_ <= 0 && _loc9_ >= -30;
         if(!this._dataArr[_loc7_].played && (_loc11_) && (this._dataArr[this._index].ad))
         {
            _loc6_ = 0;
            while(_loc6_ < _loc5_)
            {
               if(_loc6_ == _loc7_)
               {
                  this._dataArr[_loc6_].played = true;
               }
               else
               {
                  this._dataArr[_loc6_].played = false;
               }
               _loc6_++;
            }
            this._eventDispather.dispatchEvent(new ADEvent(ADEvent.INSERT_READY_TO_PLAY));
            this._eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_INSERT_REQUEST_TO_PLAY));
            setTimeout(this.waitToPlay,300);
         }
         if(!this._tipShowed && _loc9_ > 0.5 && _loc9_ <= this.SHOW_TIPS_TIME_BEFORE && !this._dataArr[_loc7_].played && (this._dataArr[this._index].ad))
         {
            this._eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_INSERT_WILL_PLAY));
            this._tipShowed = true;
         }
      }
      
      private function waitToPlay() : void {
         var _loc1_:VideoAD = this._dataArr[this._index].ad as VideoAD;
         if(_loc1_)
         {
            _loc1_.play();
            _loc1_.visible = true;
         }
      }
      
      private function createAD(param1:GroupData) : void {
         var _loc4_:* = 0;
         var _loc5_:ADData = null;
         var _loc2_:VideoAD = new VideoAD();
         _loc2_.setVolume(this._volume);
         this._mainMc.addChild(_loc2_);
         _loc2_.addEventListener(VideoADEvent.PLAY_OVER,this.onVideoPlayOver);
         _loc2_.addEventListener(VideoADEvent.PLAY_BLOCKED,this.onVideoPlayBlocked);
         _loc2_.addEventListener(VideoADEvent.PLAY_ERROR,this.onVideoPlayError);
         if(!this._width)
         {
            this._width = this._mainMc.stage.stageWidth;
            this._height = this._mainMc.stage.stageHeight;
            this._barH = 40;
         }
         var _loc3_:Array = [];
         if(param1.VAL)
         {
            _loc4_ = 0;
            while(_loc4_ < param1.VAL.length)
            {
               _loc5_ = param1.VAL[_loc4_] as ADData;
               _loc3_.push(
                  {
                     "F1":_loc5_.BRS,
                     "CU":_loc5_.CU
                  });
               _loc4_++;
            }
         }
         _loc2_.init(param1.VAL,this._context,this._eventDispather,this._width,this._height,this._barH);
         _loc2_.PIPDataArr = _loc3_;
         _loc2_.ADType = ADConstant.INSERT_AD;
         _loc2_.visible = false;
         _loc2_.preload();
         this._dataArr[this._index].ad = _loc2_;
      }
      
      private function onVideoPlayOver(param1:VideoADEvent) : void {
         trace("insertmediator.onVideoPlayOver: ");
         this._tipShowed = false;
         this._playedTime = 0;
         this._dataArr[this._index].playOverTime = getTimer();
         this._dataArr[this._index].loaded = false;
         this._dataArr[this._index].played = false;
         VideoAD(this._dataArr[this._index].ad).dispose();
         this._dataArr[this._index].ad = null;
         this._eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_VIDEO_PLAY_COMPLETE));
         this._eventDispather.dispatchEvent(new ADEvent(ADEvent.INSERT_PLAY_DONE));
      }
      
      private function onVideoPlayError(param1:VideoADEvent) : void {
         this.onDataError();
      }
      
      private function onVideoPlayBlocked(param1:VideoADEvent) : void {
         this.onVideoPlayOver(param1);
      }
      
      private function onDataError() : void {
         var _loc2_:* = 0;
         if(!this._dataArr[this._index])
         {
            return;
         }
         this._tipShowed = false;
         this._playedTime = 0;
         this._dataArr[this._index].playOverTime = getTimer();
         this._dataArr[this._index].loaded = false;
         this._dataArr[this._index].played = false;
         var _loc1_:VideoAD = this._dataArr[this._index].ad as VideoAD;
         if(_loc1_)
         {
            _loc1_.dispose();
         }
         this._dataArr[this._index].ad = null;
         if(this._dataArr[this._index].played)
         {
            this._eventDispather.dispatchEvent(new YoukuAdEvent(YoukuAdEvent.AD_VIDEO_PLAY_COMPLETE));
         }
         else
         {
            _loc2_ = 0;
            while(_loc2_ < this._dataArr.length)
            {
               this._dataArr[_loc2_].played = false;
               _loc2_++;
            }
         }
         this._eventDispather.dispatchEvent(new ADEvent(ADEvent.INSERT_PLAY_DONE));
      }
   }
}
